from collections import Counter
from app.common import mongo_client
from settings import mongo_settings


def get_orded_number(counter):
    """排序后的数字"""
    numbers = [
        {'ball': ball, 'count': count} for ball, count in counter.items()
    ]
    numbers.sort(key=lambda x: -x['count'])
    return numbers


def get_frequency_number(blue_numbers, red_numbers):
    """红 6 蓝3"""
    show_reds = set(red['ball'] for red in red_numbers)
    nshow_reds = set([f'{i:0>2}' for i in range(1, 34)]) - show_reds
    red_numbers.extend([{'ball': red, 'count': 0} for red in nshow_reds])

    show_blues = set(blue['ball'] for blue in blue_numbers)
    nshow_blues = set([f'{i:0>2}' for i in range(1, 17)]) - show_blues
    blue_numbers.extend([{'ball': blue, 'count': 0} for blue in nshow_blues])

    red_hot = red_numbers[:6]
    red_cold = red_numbers[:-7:-1]
    blue_hot = blue_numbers[:3]
    blue_cold = blue_numbers[:-4:-1]
    return {'red_hot': red_hot, 'red_cold': red_cold,
            'blue_hot': blue_hot, 'blue_cold': blue_cold}


def caculate_frequency_by_phase(phase):
    """计算期数频率 最多计算 100期
    2 8 定律
    """
    query = {'_id': {'$lte': phase}}
    projection = {'blue': 1, 'red': 1}

    item = mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_frequency].find_one,
        {'_id': phase},
        {'_id': 1})
    if item:
        return

    results = mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_lottery].find,
        query,
        projection).sort([('_id', -1)]).limit(100)

    blues = []
    reds = []

    for result in results:
        blues.append(result['blue'])
        reds.extend(result['red'])

    blues_20 = get_orded_number(Counter(blues[:20]))
    reds_20 = get_orded_number(Counter(reds[:20 * 6]))
    frequency_20 = get_frequency_number(blues_20, reds_20)

    blues_50 = get_orded_number(Counter(blues[:50]))
    reds_50 = get_orded_number(Counter(reds[:50 * 6]))
    frequency_50 = get_frequency_number(blues_50, reds_50)

    blues_80 = get_orded_number(Counter(blues[:80]))
    reds_80 = get_orded_number(Counter(reds[:80 * 6]))
    frequency_80 = get_frequency_number(blues_80, reds_80)

    blues_100 = get_orded_number(Counter(blues[:100]))
    reds_100 = get_orded_number(Counter(reds[:100 * 6]))
    frequency_100 = get_frequency_number(blues_100, reds_100)

    mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_frequency].insert,
        {'_id': phase, 'frequency_20': frequency_20,
         'frequency_50': frequency_50, 'frequency_80': frequency_80,
         'frequency_100': frequency_100}
        )


def caculate_frequency_latest():
    """计算最新一期的频率"""
    item = list(mongo_client.execute(
        mongo_client.client[mongo_settings.db_lottery][mongo_settings.col_lottery].find,
        {},
        {'_id': 1}).sort([('_id', -1)]).limit(1))[0]
    phase = item['_id']
    caculate_frequency_by_phase(phase)
